package com.tjetc.adminmanage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.tjetc.domian.Communityevent;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.CommunityeventService;
import com.tjetc.view.AdminMainView;
import com.tjetc.vo.Result;

// 活动管理菜单
public class EventManagerView {
    private static Scanner sc = new Scanner(System.in);
    private CommunityeventService eventService = ClientProxy.getProxyInstance(CommunityeventService.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public void start() {
        while (true) {
            System.out.println("活动管理菜单");
            System.out.println("1.查询活动信息");
            System.out.println("2.添加活动信息");
            System.out.println("3.修改活动信息");
            System.out.println("4.删除活动信息");
            System.out.println("5.按活动名称搜索");
            System.out.println("6.按日期查询活动");
            System.out.println("7.按时间段查询活动");
            System.out.println("8.按地点查询活动");
            System.out.println("0.返回上一级");
            switch (sc.nextInt()) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    del();
                    break;
                case 5:
                    searchByEventName();
                    break;
                case 6:
                    searchByEventDate();
                    break;
                case 7:
                    searchByDateRange();
                    break;
                case 8:
                    searchByLocation();
                    break;
                case 0:
                    new AdminMainView().start();
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void del() {
        System.out.println("删除活动信息");
        System.out.println("输入活动id");
        int id = sc.nextInt();
        Result result = eventService.del(id);
        System.out.println(result.getMsg());
    }

    private void update() {
        System.out.println("修改活动信息");
        System.out.println("输入活动id");
        int eventid = sc.nextInt();
        sc.nextLine(); // 消耗换行符
        System.out.println("输入修改后的活动名称");
        String eventname = sc.nextLine();
        System.out.println("输入修改后的活动日期 (格式: yyyy-MM-dd)");
        String eventdate = sc.nextLine();
        System.out.println("输入修改后的活动时间 (格式: HH:mm:ss)");
        String eventtime = sc.nextLine();
        System.out.println("输入修改后的活动地点");
        String location = sc.nextLine();
        System.out.println("输入修改后的活动描述");
        String description = sc.nextLine();
        try {
            Date date = dateFormat.parse(eventdate);
            Date time = timeFormat.parse(eventtime);
            Communityevent communityevent = new Communityevent(eventid, eventname, date, time, location, description);
            Result result = eventService.update(communityevent);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            System.out.println("输入的日期或时间格式错误，请按照提示格式输入！");
        }
    }

    private void add() {
        sc.nextLine(); // 消耗换行符
        System.out.println("请输入活动名称");
        String eventname = sc.nextLine();
        System.out.println("请输入活动日期 (格式: yyyy-MM-dd)");
        String eventdate = sc.nextLine();
        System.out.println("请输入活动时间 (格式: HH:mm:ss)");
        String eventime = sc.nextLine();
        System.out.println("请输入活动地点");
        String location = sc.nextLine();
        System.out.println("请输入活动描述");
        String description = sc.nextLine();

        try {
            Date date = dateFormat.parse(eventdate);
            Date time = timeFormat.parse(eventime);
            Communityevent communityevent = new Communityevent(eventname, date, time, location, description);
            Result result = eventService.add(communityevent);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            System.out.println("输入的日期或时间格式错误，请按照提示格式输入！");
        }
    }

    private void list() {
        System.out.println("活动信息如下：");
        List<Communityevent> list = eventService.list();
        if (list.isEmpty()) {
            System.out.println("暂无活动信息");
        }else{
            for (Communityevent communityevent : list) {
            System.out.println(communityevent);
        }

        }

    }

    private void searchByEventName() {
        System.out.println("请输入要搜索的活动名称（支持模糊搜索）：");
        String eventName = sc.next();
        List<Communityevent> events = eventService.findByEventNameLike(eventName);
        if (events == null || events.isEmpty()) {
            System.out.println("未找到匹配的活动信息");
        } else {
            System.out.println("查询结果：");
            for (Communityevent event : events) {
                System.out.println(event);
            }
        }
    }

    private void searchByEventDate() {
        System.out.println("请输入要查询的日期（格式：yyyy-MM-dd）：");
        String dateStr = sc.next();
        try {
            dateFormat.parse(dateStr); // 验证日期格式
            List<Communityevent> events = eventService.findByEventDate(dateStr);
            if (events == null || events.isEmpty()) {
                System.out.println("未找到该日期的活动信息");
            } else {
                System.out.println("查询结果：");
                for (Communityevent event : events) {
                    System.out.println(event);
                }
            }
        } catch (ParseException e) {
            System.out.println("日期格式错误，请使用yyyy-MM-dd格式");
        }
    }

    private void searchByDateRange() {
        System.out.println("请输入开始日期（格式：yyyy-MM-dd）：");
        String startDate = sc.next();
        System.out.println("请输入结束日期（格式：yyyy-MM-dd）：");
        String endDate = sc.next();
        try {
            dateFormat.parse(startDate); // 验证日期格式
            dateFormat.parse(endDate); // 验证日期格式
            List<Communityevent> events = eventService.findByDateRange(startDate, endDate);
            if (events == null || events.isEmpty()) {
                System.out.println("未找到该时间段的活动信息");
            } else {
                System.out.println("查询结果：");
                for (Communityevent event : events) {
                    System.out.println(event);
                }
            }
        } catch (ParseException e) {
            System.out.println("日期格式错误，请使用yyyy-MM-dd格式");
        }
    }

    private void searchByLocation() {
        System.out.println("请输入要搜索的活动地点（支持模糊搜索）：");
        String location = sc.next();
        List<Communityevent> events = eventService.findByLocationLike(location);
        if (events == null || events.isEmpty()) {
            System.out.println("未找到匹配的活动信息");
        } else {
            System.out.println("查询结果：");
            for (Communityevent event : events) {
                System.out.println(event);
            }
        }
    }
}
